var luid=localStorage.getItem("luid");
var playxinxi={guanka:'',map:'',guankaid:'',guankaqian:''}//关卡信息
function ScheduleScene(){
            this.$me = $("<div class='scene'></div>");
            this.$sai1=$("<div class='sai1'></div>");
            this.$zuoqie=$("<a href='#' id='zuoqie'></a>");//设置左切箭头
            this.$main=$("<div id='main'></div>");//关卡主页面
            this.$youqie=$("<a href='#' id='youqie'></a>"); //设置右切箭头          
            this.$yes=$("<button class='yes'>确定</button>");
            this.$sai1.append(this.$zuoqie);
            this.$sai1.append(this.$main);
            this.$sai1.append(this.$youqie);
            this.$sai1.append(this.$yes);
            this.$me.append(this.$sai1);
            var simg=0;
            var self = this;
                 var jsonobj={"handleid":10}; 
                  $.ajax({
                    type:"post",
                    url:"handle/handout.php",
                   dataType:"json",
                    data:jsonobj,
                    success:function(data){
                     //alert(data);
                      var rs_arr=[];
                      $(data).each(function(i,val){
                        var rs=[];
                        for(var prop in val){
                          rs.push(val[prop]);
                        }
                        rs_arr.push(rs);
                        console.log(rs_arr);
                      });
                      //错误的情况
                      if(rs_arr[0][0]=='error'){
                       
                       alert(data['errorinf']);

                      }     
                      //正确的情况
                      if(rs_arr[0][0]=='success'){
//****************************显示赛事****************************
var $bannerimgs=[];
for (var i= 0;i<data['saishi'].length;i++) {   
    $bannerimgs.push(data['saishi'][i]['img']);      
    }  
    console.log($bannerimgs);     
    var $nowimg = 0;
    simg=$bannerimgs[$nowimg];
     self.turnright = function(){
        $nowimg = ($nowimg+1)%data['saishi'].length;
        self.$main.css("background-image" , "url("+$bannerimgs[$nowimg]+")");
        return simg=$bannerimgs[$nowimg];;
    }//右切图片跳转
     self.turnleft = function(){
        $nowimg = ($nowimg+2)%data['saishi'].length;
        self.$main.css("background-image" , "url("+$bannerimgs[$nowimg]+")");          
        return simg=$bannerimgs[$nowimg];; 
     }//左切图片跳转
    self.$youqie.click(function() {
        self.turnright();//调用类里面的函数除了要用self也要加括号
    }); 
    self.$zuoqie.click(function() {
        self.turnleft();
    });
        }
       },    
 error:function(responexml,name){
   alert("saishi"+name);
        }
     });
   
      
//****************************确定赛事****************************
	this.$yes.click(function() {
	var jsonobj={"handleid":9,"simg":simg,"userid":luid};
	 console.log(simg);
	   $.ajax({
	     type:"post",
	     url:"handle/handout.php",
	     dataType:"json",
	     data:jsonobj,
	     success:function(data){
	      //alert(data);
	       var rs_arr=[];
	       $(data).each(function(i,val){
	         var rs=[];
	         for(var prop in val){
	           rs.push(val[prop]);
	           //console.log(val[prop]);
	         }
	         //console.log(rs+"----");
	         rs_arr.push(rs);
	         console.log(rs_arr);
	       });
	       //错误的情况
	       if(rs_arr[0][0]=='error'){                      
	       alert("saishi"+data['errorinf']);
	       }     
	       //正确的情况
	       if(rs_arr[0][0]!=='error'){
	       self.$me.empty();
	         //循环创建列表
	        for(var i=0;i<rs_arr.length;i++){	
	            var saiduan=new SaiduanScene(rs_arr[i][1]);//创建赛段
	            console.log( rs_arr.length);
	            console.log(rs_arr[i][1]);
	            saiduan.guanka=rs_arr[i][2];//当前关卡名称
	            saiduan.map=rs_arr[i][3];//当前关卡地图
	            saiduan.guankaid=rs_arr[i][0];//当前关卡id
	            saiduan.guankaqian=rs_arr[i][4];//当前关卡可获金币
	            //console.log(saiduan.guankaqian);
	            self.$me.append(saiduan.$duan1);
	            if(rs_arr[i][5]==null){
	                saiduan.$duan1.append(saiduan.$suo1);//加锁
	                saiduan.$buguoguan();//不过关
	            }
	            saiduan.$guoguan();//过关跳转
	            self.$me.append(saiduan.$but);//退出
	            self.$me.append(saiduan.$duan);
//	            localStorage.setItem("map",rs_arr[i][3]);
//	            localStorage.setItem("guanka",rs_arr[i][2]);
//	            localStorage.setItem("guankaid",rs_arr[i][0]);
//	            localStorage.setItem("guankaqian",rs_arr[i][4]);
	        }
	           // 退出按钮
	        self.$but=$("<a href='#' id='retur'  class='retur' ></a>");
	        self.$me.append(self.$but);
	
	        self.$but.click(function(){
	                director.runScene(new MainScene());
	                });
	        } 
	     },
	      error:function(responexml,name){
	        alert("saiduan"+name);
	         }
	      });
	   });
}
function SaiduanScene(img){
    var self=this;
    this.$duan=$("<button class='duan1'>赛段选择</button>");
    this.$duan1=$("<a href='#' class='duan' style='background-image: url("+img+");' ></a>");
    this.$suo1=$("<div class='suo' style='' ></div>");
   // this.$suo2=$("<div class='suo' style='' ></div>");
   // this.$suo3=$("<div class='suo' style='' ></div>");

    this.$guoguan=function(){
        this.$duan1.on("click", function(){
            playxinxi.guanka=self.guanka;
            playxinxi.map=self.map;
            playxinxi.guankaid=self.guankaid;
            playxinxi.guankaqian=self.guankaqian;
            director.runScene(new MineScene());
        });
    }
    this.$buguoguan=function(){
        $(".suo").on("click", function(){
            alert("此关卡未解锁");
        });
    }
    //退出按钮
    this.$but=$("<a href='#' id='retur'  class='retur' ></a>");


    this.$but.click(function(){
        director.runScene(new ScheduleScene());
    });
    //this.$saiduan.append(this.$duan1);
}